/*
 * ActivityApp.java
 */

package procrastinator;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import procrastinator.mac.MacIntegration;

/**
 * The main class of the application.
 */
public class Main extends SingleFrameApplication {
    private JProcrastinatorFrame view;
    private DatabaseConnection db;
    
    public Main() {
        super();
        
        try {
            db = new DatabaseConnection("ProcrastinatorDB");
        } catch (ClassNotFoundException ex) {
            System.err.println("FATAL ERROR: Couldn't load JavaDB database driver!");
            ex.printStackTrace(System.err);
            System.exit(100);
        } catch (SQLException ex) {
            System.err.println("FATAL ERROR: Couldn't open or create Procrastinator database!");
            ex.printStackTrace(System.err);
            System.exit(101);
        }
        
    }
            
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        view = new JProcrastinatorFrame(this);
        show(view);
        view.initFocus();
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of ActivityApp
     */
    public static Main getApplication() {
        return Application.getInstance(Main.class);
    }
    
    public DatabaseConnection getConnection() {
        return db;
    }
    
    public JProcrastinatorFrame getMainWindow() {
        return view;
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        MacIntegration.integrate();
        launch(Main.class, args);
    }
}
